package com.sphd.beautysalon.activitys.vip;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.sphd.beautysalon.R;
import com.sphd.beautysalon.adapters.VipAddMoneyAdapter;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.RecordTopUpInfo;
import com.sphd.beautysalon.info.TopUpInfo;
import com.sphd.beautysalon.info.VipInfo;
import com.sphd.beautysalon.presenter.VipAddPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.VipAddView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 会员充值
* */
public class VipAddmoneyActivity extends BaseActivity implements VipAddView {

@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.account_money)
TextView accountMoney;
@BindView(R.id.vip_money_spinner)
Spinner vipMoneySpinner;
@BindView(R.id.vip_add_money_update)
Button vipAddMoneyUpdate;
@BindView(R.id.manager_list_topup_recyclerview)
RecyclerView managerListTopupRecyclerview;
Intent intent;
Bundle bundle;
VipInfo vipInfo;
VipAddPresenterImpl vipAddPresenter;
List<String> strings;
ArrayAdapter arrayAdapter;
List<TopUpInfo> topUpInfos;
VipAddMoneyAdapter vipAddMoneyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vip_addmoney);
    ButterKnife.bind(this);
    initView();
    initEvent();
}

private void initEvent() {
    toolbar.setNavigationOnClickListener(view -> {
        setResult(0);
        finish();
    });
}

private void initView() {
    setTitleBar(toolbar, "充值中心");
    intent = getIntent();
    bundle = intent.getExtras();
    vipInfo = (VipInfo) bundle.getSerializable(Constant.VIPINFO);
    vipAddPresenter = new VipAddPresenterImpl(this);
    accountMoney.setText("账户剩余金额："+turn(vipInfo.getMem_account())+"元");
    initAdapter();
    getRech();
    getRechList();
}

private void getRechList() {
    vipAddPresenter.getRecodList(RestaurantUrl.Httptitle + RestaurantUrl.VIPRECHARGELIST+"cgid="+
            vipInfo.getMem_cgid()+"&card="+vipInfo.getMem_card());
}

private void initAdapter() {
    strings=new ArrayList<>();
    arrayAdapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings);
    vipMoneySpinner.setAdapter(arrayAdapter);
    setRecy(managerListTopupRecyclerview);
    vipAddMoneyAdapter=new VipAddMoneyAdapter(R.layout.item_add_money,new ArrayList<>());
    managerListTopupRecyclerview.setAdapter(vipAddMoneyAdapter);
}

private void getRech() {
    vipAddPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.CzList + "cgid=" + vipInfo.getMem_cgid());

}

@OnClick(R.id.vip_add_money_update)
public void onClick() {
   int pos= vipMoneySpinner.getSelectedItemPosition();

   vipAddPresenter.topup(RestaurantUrl.Httptitle + RestaurantUrl.VipAddMoney +
            "price=" + topUpInfos.get(pos).getAdd_money() + "&cgid="
            + vipInfo.getMem_cgid() + "&cgname=" + vipInfo.getMem_cgname() + "&memPhone=" + vipInfo.getMem_phone()
            + "&money=" + topUpInfos.get(pos).getReceipt_money() + "&card=" + vipInfo.getMem_card(),topUpInfos.get(pos).getAdd_money());
}

@Override
public void recList(List<TopUpInfo> topUpInfos) {
    if (topUpInfos.size()==0){
        vipMoneySpinner.setVisibility(View.GONE);
        vipAddMoneyUpdate.setVisibility(View.GONE);
    }else {
        this.topUpInfos=topUpInfos;
        for (TopUpInfo topUpInfo:topUpInfos){
            strings.add("缴费"+topUpInfo.getReceipt_money()+"充值"+
                    topUpInfo.getAdd_money());
        }
        arrayAdapter.notifyDataSetChanged();
    }

}

@Override
public void recRes(int res,int add_money) {
    if (res==1){
        toast("充值成功");
        getRechList();
    }else {
        toast("充值失败，请重试");
    }
}

@Override
public void recResList(double account, List<RecordTopUpInfo> recordTopUpInfos) {
    accountMoney.setText("账户剩余金额："+turn(account)+"元");
    vipAddMoneyAdapter.setNewData(recordTopUpInfos);
}

@Override
public void showLoading() {
    showLoadingDialog();
}

@Override
public void dissLoading() {
    dissLoadingDialog();
}
}
